package com.wenwan.manage.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.StorageClass;
import com.qcloud.cos.region.Region;
import com.wenwan.manage.service.FileService;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.setting.WxytSetting;
import com.wenwan.manage.util.Util;

@Service
public class FileServiceImpl implements FileService {

	// @Override
	// public DataToObject<String> uploadOneFile(MultipartFile multipartFile) {
	// // TODO Auto-generated method stub
	// return Util.uploadOneFile(multipartFile);
	// }
	//
	// @Override
	// public Boolean deleteOneFile(String url) {
	// // TODO Auto-generated method stub
	// return null;
	// }
	//
	// @Override
	// public List<String> uploadMoreFile(MultipartFile[] multipartFile) {
	// // TODO Auto-generated method stub
	// return null;
	// }
	//
	// @Override
	// public List<Boolean> deleteMoreFile(String urlList) {
	// // TODO Auto-generated method stub
	// return null;
	// }

	@Override
	public Map<String, Object> uploadOneFile(byte[] bytes, String imageName) {
		// TODO Auto-generated method stub
		Map<String, Object> dto = new HashMap<String, Object>();
		String suffix = imageName.substring(imageName.lastIndexOf("."), imageName.length());

		String name = Util.UUID() + suffix;

		SimpleUploadFileFromStream(byte2Input(bytes), "image/jpeg", name);

		// dto.setData(WxytSetting.BASE_URL + name);
		dto.put("data", WxytSetting.BASE_URL + name);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	public static final InputStream byte2Input(byte[] buf) {
		return new ByteArrayInputStream(buf);
	}

	public String SimpleUploadFileFromStream(InputStream inputStream, String contentType, String imageName) {

		COSCredentials cred = new BasicCOSCredentials(WxytSetting.SECRET_ID, WxytSetting.SECRET_KEY);
		// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region(WxytSetting.REGION_NAME));
		// 3 生成cos客户端
		COSClient cosclient = new COSClient(cred, clientConfig);

		String bucketName = WxytSetting.BUCKET_NAME2;

		String etag = "";

		try {

			ObjectMetadata objectMetadata = new ObjectMetadata();
			// 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据，存在内存OOM的情况
			objectMetadata.setContentLength(inputStream.available());
			// 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
			// objectMetadata.setContentType(contentType);

			PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, imageName, inputStream,
					objectMetadata);
			// 设置存储类型, 默认是标准(Standard), 低频(standard_ia), 近线(nearline)
			putObjectRequest.setStorageClass(StorageClass.Standard);

			PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
			// putobjectResult会返回文件的etag
			etag = putObjectResult.getETag();
			System.out.println(etag);
		} catch (CosServiceException e) {
			e.printStackTrace();
		} catch (CosClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 关闭客户端
		cosclient.shutdown();

		return etag;
	}

}
